package com.zzh.controller;

import com.zzh.domain.Course;
import com.zzh.domain.CourseVO;
import com.zzh.domain.ResponseResult;
import com.zzh.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping("/findCourseByCondition")
    public ResponseResult findCourseByCondition(@RequestBody CourseVO courseVO) {
        List<Course> courseList = courseService.findCourseByCondition(courseVO);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", courseList);
        return result;
    }

    @RequestMapping("/courseUpload")
    public ResponseResult courseUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            if (file.isEmpty()) {
                throw new RuntimeException("文件是null的");
            }
            String realPath = request.getServletContext().getRealPath("/");
            String webappPath = realPath.substring(0, realPath.indexOf("zzh_edu_ssm_web"));
            String fileName = file.getOriginalFilename();
            String newFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
            String uploadPath = webappPath + "upload//";
            File filePath = new File(uploadPath, newFileName);
            if (!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
            }
            file.transferTo(filePath);
            Map<String, String> map = new HashMap<>();
            map.put("fileName", newFileName);
            map.put("filePath", "http://localhost:8080" + "/upload/" + newFileName);
            ResponseResult result = new ResponseResult(true, 200, "上传文件成功", map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/saveOrUpdateCourse")
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVO courseVO) {
        if (courseVO.getId() == null) {
            //新增
            courseService.saveCourseOrTeacher(courseVO);
            return new ResponseResult(true, 200, "响应成功", null);
        } else {
            //修改
            courseService.updateCourseOrTeacher(courseVO);
            return new ResponseResult(true, 200, "响应成功", null);
        }
    }

    @RequestMapping("/findCourseById")
    public ResponseResult findCourseById(@RequestParam int id) {
        CourseVO courseVO = courseService.findCourseById(id);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", courseVO);
        return result;
    }

    @RequestMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus(@RequestParam int id, @RequestParam int status) {
        courseService.updateCourseStatus(id, status);
        Map<String, Integer> map = new HashMap<>();
        map.put("status", status);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
        return result;
    }
}
